package com.leetcood;

public class SortColor {	
	public static void main(String[] args) {
		int[] nums = {0,1,2,1,1,0,2};
		SortColor sc = new SortColor();
		sc.sortColors(nums);
		for(int i : nums)
			System.out.print(i);
	}
	public void sortColors(int[] nums) {
        int begin = 0;
        int curr = 0;
        int end = nums.length - 1;
        int temp = 0;
        while (curr <= end) {
            if (nums[curr] == 0) {
                if (begin != curr) {
                    temp = nums[begin];
                    nums[begin] = nums[curr];
                    nums[curr] = temp;
                }
                begin++;
                curr++;
            } else if (nums[curr] == 1) {
                curr++;
            } else if (nums[curr] == 2) {
                if (end != curr) {
                    temp = nums[end];
                    nums[end] = nums[curr];
                    nums[curr] = temp;
                }
                end--;
            }
        }
         
    }
}
